package com.hmjk.health.card;

import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;

import com.hmjk.health.HealthApp;
import com.hmjk.health.R;
import com.hmjk.health.util.LOCATION_INFO;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.format.Time;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;


public class AddressListDialog extends Dialog implements android.view.View.OnClickListener{
	private Context mContext;
	private static AddressListDialog mSelf;
	private HealthApp mApp = null;
	private ListView mList;
	private static ArrayList<JSONObject> mArray;
	
	protected AddressListDialog(Context context, int theme) {
		super(context,theme);
		mContext = context;
		mApp = (HealthApp) mContext.getApplicationContext();
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		LayoutInflater inflater = LayoutInflater.from(mContext);
		View v = inflater.inflate(R.layout.address_list_pop, null);
		setContentView(v);
		
		mList = (ListView) v.findViewById(R.id.list);
		mList.setAdapter(new AddressAdapter());
		View cancel = v.findViewById(R.id.but_cancel);
		cancel.setOnClickListener(this);
		Window win = getWindow();
		win.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
		win.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN|WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
	
	}
	
	class AddressAdapter extends BaseAdapter{
		class ViewHolder{
			public TextView address;
			public TextView timedate;
			public ImageView sos;
		}
		
		@Override
		public int getCount() {
			return mArray.size();
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int pos, View itemView, ViewGroup group) {
			ViewHolder holder;
			
			if(itemView == null){
				LayoutInflater inflater = LayoutInflater.from(mContext);
				itemView = inflater.inflate(R.layout.pop_address_item, null);
				holder = new ViewHolder();
				
				holder.address = (TextView)itemView.findViewById(R.id.item_address);
				holder.timedate = (TextView)itemView.findViewById(R.id.item_time);
				holder.sos = (ImageView) itemView.findViewById(R.id.sos);
				itemView.setTag(holder);
			}else{
				holder = (ViewHolder)itemView.getTag();
			}
			
			try {
				JSONObject e = mArray.get(pos);
				String addr = "";
				if(e.has("addr"))
				  addr = e.getString("addr");
				long time = e.getLong("time");
				Time t = new Time();
				t.setToNow();
				t.set(time);
				String monstr = String.format("%02d", t.month+1);
				String daystr = String.format("%02d", t.monthDay);
				String hourstr = String.format("%02d", t.hour);
				String minutestr = String.format("%02d", t.minute);
				String secondstr = String.format("%02d", t.second);
				String timestr = mContext.getString(R.string.addr_date_time, t.year, monstr, daystr
						,hourstr, minutestr, secondstr);
				holder.address.setText(addr);
				holder.timedate.setText(timestr);
				int flag = e.getInt("flag");
				if((flag & LOCATION_INFO.FLAG_SOS) == LOCATION_INFO.FLAG_SOS){
					holder.sos.setVisibility(View.VISIBLE);
				}else{
					holder.sos.setVisibility(View.GONE);
				}
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			
			
			return itemView;
		}
		
	}
	
	public static void open(Context context, ArrayList<JSONObject> array){
		if(mSelf == null){
			mSelf = new AddressListDialog(context, R.style.CustomDialog);
		}
		mSelf.setCanceledOnTouchOutside(false);
		mSelf.setCancelable(true);
		mSelf.setOnDismissListener(new OnDismissListener(){

			@Override
			public void onDismiss(DialogInterface arg0) {
				mSelf = null;
			}
			
		});
		mArray = array;

		mSelf.show();
	}

	@Override
	public void onClick(View arg0) {
		mSelf.dismiss();
	}
}
